//
//  StatusCell.swift
//  weibo
//
//  Created by cmh on 15/9/6.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit

// 控件间距
let statusCellMargin: CGFloat = 12
// 头像大小
let statusCellIconWidth: CGFloat = 35
// 图片的默认大小
let statusCellPictureWidth: CGFloat = 90
// 图片的间距
let statusCellPictureMargin: CGFloat = 10
// 图片每行最大3张
let statusCellPictureMaxCount: CGFloat = 3
// 图像的最大宽度
let statusCellPictureMaxWidth: CGFloat = statusCellPictureWidth * statusCellPictureMaxCount + (statusCellPictureMaxCount - 1) * statusCellPictureMargin

class StatusCell: UITableViewCell {
    
    /// 微博数据视图模型
    var statusRowViewModel: StatusRowViewModel? {
        didSet {
            // 模型数值被设置之后，马上要产生的连锁反应 - 界面UI发生变化
            topView.statusRowViewModel = statusRowViewModel
            
            centerText.text = statusRowViewModel?.status.text
            
            pictureView.statusRowViewModel = statusRowViewModel
            
            pictureViewWidth?.constant = pictureView.bounds.width
            pictureViewHeight?.constant = pictureView.bounds.height
            pictureViewTop?.constant = statusRowViewModel?.thumbnailURLs?.count == 0 ? 0 : statusCellMargin
            
            
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 计算cell的高度
    func rowHeight(viewModel: StatusRowViewModel) -> CGFloat {
        
        statusRowViewModel = viewModel
        
        // 更新所有控件的frame
        layoutIfNeeded()
        
        // 返回底部视图的最大y值，就是cell的高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    // 记录配图图片的宽高及顶部约束
    var pictureViewWidth: NSLayoutConstraint?
    var pictureViewHeight: NSLayoutConstraint?
    var pictureViewTop: NSLayoutConstraint?
    
    func setupUI() {
        // 顶部分割视图
        let topSepView = UIView()
        topSepView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)

        // 1. 添加控件
        contentView.addSubview(topSepView)
        contentView.addSubview(topView)
        contentView.addSubview(centerText)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)

        
        // 2. 设置布局，cell 的默认大小是 320/44，iPhone 4时代的遗留问题，宽度不能直接使用cell的宽度
        let width = UIScreen.mainScreen().bounds.width
        // 顶部灰条
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))

        // 顶部视图
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSepView, size: CGSizeMake(width, statusCellIconWidth + statusCellMargin))
        
        // 中间文本
        centerText.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPointMake(statusCellMargin, statusCellMargin))

        // 底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPointMake(-statusCellMargin, statusCellMargin))
        
    }
    
    // MARK: 懒加载控件
    // 1. 顶部视图
    private lazy var topView: StatusTopView = StatusTopView()
    
    // 2.中间文本
    lazy var centerText = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 14, layoutWidth: UIScreen.mainScreen().bounds.width - 2 * statusCellMargin)
    
    // 3.图片
    lazy var pictureView: StatusPictureView = StatusPictureView()
    
    // 4.底部视图
    lazy var bottomView: StatusBottomView = StatusBottomView()

   
}
